home *** CD-ROM | disk | FTP | other *** search
- // send <'signature'|name> <string>
- tclSend(clientData, interp, argc, argv)
- ClientData clientData;
- Tcl_Interp *interp;
- int argc;
- char **argv;
- {
- ProcessSerialNumber process;
- AppleEvent theEvent, reply;
- AEAddressDesc address;
- long sig = 0, retType;
- OSErr res;
- Size sz;
- ProcessInfoRec infoRec;
-
- if (argc != 3)
- {
- sprintf(interp->result, "Usage: %s <'signature'|name> <string>", argv[0]);
- return TCL_ERROR;
- }
-
- if (argv[1][0] == '\'')
- {
- memmove(&sig, argv[1] + 1, 4);
- if (res = AECreateDesc(typeApplSignature, &sig, sizeof(sig), &address))
- {
- sprintf(interp->result, "Create desc: %d", res);
- return TCL_ERROR;
- }
- }
- else
- {
- char name[256];
- FSSpec spec;
-
- process.highLongOfPSN = 0;
- process.lowLongOfPSN = kNoProcess;
- infoRec.processInfoLength = sizeof(ProcessInfoRec);
- infoRec.processName = name;
- infoRec.processAppSpec = &spec;
- CtoPstr(argv[1]);
- while ((res = GetNextProcess(&process)) == noErr)
- {
- if (GetProcessInformation(&process, &infoRec) == noErr)
- {
- if (!pStrcmp(argv[1], infoRec.processName))
- {
- break;
- }
- }
- }
- PtoCstr(argv[1]);
- if (res)
- {
- sprintf(interp->result, "No process named '%s'", argv[1]);
- return TCL_ERROR;
- }
- if (res = AECreateDesc(typeProcessSerialNumber, &process, sizeof(process), &address))
- {
- sprintf(interp->result, "Create desc: %d", res);
- return TCL_ERROR;
- }
- }
-
- if (res = AECreateAppleEvent(kTclClass, kAEEval, &address,
- kAutoGenerateReturnID, kAnyTransactionID, &theEvent))
- {
- sprintf(interp->result, "Create apple event err: %d", res);
- return TCL_ERROR;
- }
-
- if (res = AEPutParamPtr(&theEvent, keyDirectObject, typeCString, argv[2],
- strlen(argv[2]) + 1))
- {
- sprintf(interp->result, "AEPut parameter error: %d", res);
- return TCL_ERROR;
- }
-
- if (res = AESend(&theEvent, &reply, kAEWaitReply + kAECanInteract,
- kAENormalPriority, 1200, NULL, NULL))
- {
- sprintf(interp->result, "AESend error: %d", res);
- return TCL_ERROR;
- }
-
- if (res = AEGetParamPtr(&reply, keyDirectObject, typeCString, &retType,
- interp->result, TCL_RESULT_SIZE, &sz))
- {
- sprintf(interp->result,"AEGetParam error: %d", res);
- return TCL_ERROR;
- }
-
- return TCL_OK;
- }
-
-
- pascal OSErr
- aeTclEval(event, reply, ref)
- AppleEvent *event, *reply;
- long ref;
- {
- AEDescList docList;
- OSErr res;
- OSErr retVal;
- long index, itemsInList;
- long sz;
- AEKeyword keyWord;
- DescType returnedType;
- char buffer[256];
-
- if (res = AEGetParamDesc(event, keyDirectObject, typeAEList, &docList))
- {
- SysBeep(1);
- return res;
- }
-
- /* check for missing params */
- if (res = MyGotRequiredParams(event))
- {
- SysBeep(1);
- return res;
- }
-
- if (res = AEGetNthPtr(&docList, 1, typeCString, &keyWord,
- &returnedType, buffer, sizeof(buffer),&sz))
- {
- SysBeep(1);
- return res;
- }
- AEDisposeDesc(&docList);
-
- // Evaluate the string and return the result.
- retVal = Tcl_Eval(interp, buffer, 0, NULL);
- res = AEPutParamPtr(reply, keyDirectObject, typeCString, interp->result,
- strlen(interp->result) + 1);
- return retVal;
- }
-
-
-
- tclSwitch(clientData, interp, argc, argv)
- ClientData clientData;
- Tcl_Interp *interp;
- int argc;
- char **argv;
- {
- ProcessSerialNumber process;
- ProcessInfoRec infoRec;
- FSSpec spec;
- char name[256];
- long sig;
-
- if (argc != 2)
- {
- sprintf(interp->result, "Usage: %s <name-of-app|'signature'>", argv[0]);
- return TCL_ERROR;
- }
- if (argv[1][0] == '\'')
- {
- strncpy(&sig, argv[1] + 1, 4L);
- }
- else
- {
- sig = 0L;
- CtoPstr(argv[1]);
- }
-
- process.highLongOfPSN = 0;
- process.lowLongOfPSN = kNoProcess;
- infoRec.processInfoLength = sizeof(ProcessInfoRec);
- infoRec.processName = name;
- infoRec.processAppSpec = &spec;
- while (GetNextProcess(&process) == noErr)
- {
- if (GetProcessInformation(&process, &infoRec) == noErr)
- {
- if (sig)
- {
- if ((infoRec.processType == 'APPL') &&
- (infoRec.processSignature == sig))
- {
- SetFrontProcess(&process);
- return TCL_OK;
- }
- }
- else
- {
- if (!pStrcmp(argv[1], infoRec.processName))
- {
- SetFrontProcess(&process);
- return TCL_OK;
- }
- }
- }
- }
- sprintf(interp->result, "Unable to find your process");
- return TCL_ERROR;
- }
-
-